package com.z.sys.controller;

import com.github.pagehelper.PageInfo;
import com.z.sys.entity.BackConfig;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.aop.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import com.z.common.aop.enums.BusinessType;
import com.z.common.web.vo.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.z.sys.service.IBackConfigService;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;

/**
 * 备份配置Controller
 *
 * @author z
 * @date 2023-12-27
 */
@Api(tags = "备份配置")
@RestController
@RequestMapping("/system/backConfig")
public class BackConfigController extends BaseController {

    private String prefix = "system/backConfig";

    @Autowired
    private IBackConfigService backConfigService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/sys/backConfig/main','sys:backConfig:main')")
    @ApiOperation(value = "查询页面")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询备份配置列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/sys/backConfig/data','sys:backConfig:main')")
    @Log(title = "备份配置", describe = "查询备份配置列表", type = BusinessType.QUERY)
    @ApiOperation(value = "查询接口")
    public Result list(@ModelAttribute BackConfig backConfig, PageVo pageVo) {
        PageInfo<BackConfig> pageInfo = backConfigService.selectBackConfigPage(backConfig, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增备份配置
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/sys/backConfig/add','sys:backConfig:add')")
    @ApiOperation(value = "添加页面")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增备份配置
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/sys/backConfig/save','sys:backConfig:add')")
    @Log(title = "备份配置", describe = "新增备份配置数据", type = BusinessType.ADD)
    @ApiOperation(value = "添加接口")
    public Result save(@RequestBody BackConfig backConfig) {
        return Result.decide(backConfigService.save(backConfig));
    }

    /**
     * 修改备份配置
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/sys/backConfig/edit','sys:backConfig:edit')")
    @ApiOperation(value = "修改页面")
    public ModelAndView edit(Long id, ModelMap map) {
        BackConfig backConfig =backConfigService.getById(id);
        map.put("backConfig", backConfig);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改备份配置
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/sys/backConfig/update','sys:backConfig:edit')")
    @Log(title = "备份配置", describe = "修改备份配置数据", type = BusinessType.EDIT)
    @ApiOperation(value = "修改接口")
    public Result update(@RequestBody BackConfig backConfig) {
        return Result.decide(backConfigService.updateById(backConfig));
    }

    /**
     * 删除备份配置
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/sys/backConfig/batchRemove','sys:backConfig:del')")
    @Log(title = "备份配置", describe = "删除备份配置数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "批量删除接口")
    public Result batchRemove(String ids) {
        return Result.decide(backConfigService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除备份配置
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/sys/backConfig/remove','sys:backConfig:del')")
    @Log(title = "备份配置", describe = "删除备份配置数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "删除接口")
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(backConfigService.removeById(id));
    }

    /**
     * 删除备份配置
     */
    @ResponseBody
    @PostMapping("/backDir/{id}")
    @PreAuthorize("hasPermission('/sys/backConfig/backDir','sys:backConfig:edit')")
    @Log(title = "备份配置", describe = "备份目录", type = BusinessType.EXPORT)
    @ApiOperation(value = "备份接口")
    public Result backDir(@PathVariable("id") Long id) {
        BackConfig backConfig = backConfigService.getById(id);
        backConfigService.backDir(backConfig.getBackDir(), backConfig.getOutDir());
        return Result.success();
    }



}
